import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _split from "lodash/split";
import _omit from "lodash/omit";
import _map from "lodash/map";
import _isFunction from "lodash/isFunction";
import _forEach from "lodash/forEach";
var _excluded = ["fields", "context"];

var _dec, _class, _class2;

import React from 'react';
import querystring from 'querystring';
import { withRouter } from 'dva/router';
import { Bind } from 'lodash-decorators';
import { dealButtonProps, getBtnType } from "./utils";
import { ACTION_CODE, dynamicToolBarOmitProps, fieldLabelProp, fieldNameProp, PAGE_PARAM, subEventSep } from "../config";
import DynamicModal from "../DynamicModal";
import { getContextValue } from "../utils";
import { openPageModalBodyStyle, openTypeCode } from "../DynamicTable/utils";
var DynamicToolbar = (_dec = Bind(), withRouter(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicToolbar, _React$Component);

  var _super = _createSuper(DynamicToolbar);

  function DynamicToolbar() {
    var _this;

    _classCallCheck(this, DynamicToolbar);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {};
    _this.pageModalRef = void 0;
    return _this;
  }

  _createClass(DynamicToolbar, [{
    key: "handlePageModalRef",
    value: function handlePageModalRef(pageModalRef) {
      this.pageModalRef = pageModalRef;
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      var onRef = this.props.onRef;

      if (_isFunction(onRef)) {
        onRef(this);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var otherProps = _omit(this.props, dynamicToolBarOmitProps);

      return this.renderComposeToolBar(otherProps);
    }
  }, {
    key: "renderComposeToolBar",
    value: function renderComposeToolBar(_ref) {
      var fields = _ref.fields,
          context = _ref.context,
          toolbarProps = _objectWithoutProperties(_ref, _excluded);

      // toolbarProps 已经在 DynamicComponent 中处理了
      var children = this.renderToolbarFields({
        fields: fields,
        context: context
      });
      var _this$state$pageModal = this.state.pageModalProps,
          pageModalProps = _this$state$pageModal === void 0 ? {} : _this$state$pageModal; // 渲染 Toolbar 组件的属性与render

      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", toolbarProps, children), /*#__PURE__*/React.createElement(DynamicModal, Object.assign({}, pageModalProps, {
        key: "pageModal",
        onRef: this.handlePageModalRef
      })));
    }
  }, {
    key: "renderToolbarFields",
    value: function renderToolbarFields(_ref2) {
      var _this2 = this;

      var _ref2$fields = _ref2.fields,
          fields = _ref2$fields === void 0 ? [] : _ref2$fields,
          context = _ref2.context;
      return _map(fields, function (field) {
        // 获取组件不依赖 Context 的属性
        // 获取对应的组件
        var ComponentType = getBtnType({
          field: field
        });

        var _dealButtonProps = dealButtonProps({
          field: field,
          context: context
        }),
            componentProps = _dealButtonProps.componentProps,
            _dealButtonProps$btn = _dealButtonProps.btn,
            btnProps = _dealButtonProps$btn === void 0 ? {} : _dealButtonProps$btn;

        var buttonProps = {};

        switch (btnProps.action) {
          case ACTION_CODE.action:
            buttonProps.onClick = function () {
              var actionEvent = btnProps.actionEvent;
              var func = getContextValue(context, actionEvent);

              if (_isFunction(func)) {
                func().catch(function
                  /* e */
                () {// todo 失败不需要做事情?
                  // console.error(e);
                });
              }
            };

            break;

          case ACTION_CODE.page:
            buttonProps.onClick = function () {
              var modalProps = {};
              var params = {};
              var search = _this2.props.history.search;
              var urlParams = querystring.parse(search);
              var paramStream = (btnProps.params || '').split(',');

              for (var i = 1; i <= paramStream.length; i++) {
                if (i % 3 === 0) {
                  switch (paramStream[i - 2]) {
                    case PAGE_PARAM.fixParam:
                      params[paramStream[i - 3]] = paramStream[i - 1];
                      break;

                    case PAGE_PARAM.urlParam:
                      params[paramStream[i - 1]] = urlParams[paramStream[i - 3]];
                      break;

                    default:
                      break;
                  }
                }
              } // 打开 Modal 或 页面


              switch (btnProps.openType) {
                case openTypeCode.inner:
                  // 跳转
                  break;

                case openTypeCode.modal:
                  // 打开 Modal
                  modalProps.type = openTypeCode.modal;
                  modalProps.bodyStyle = openPageModalBodyStyle[btnProps.openPageTypeModal].bodyStyle;
                  modalProps.width = openPageModalBodyStyle[btnProps.openPageTypeModal].width;
                  modalProps.modalBtns = btnProps.modalBtns; // modal 按钮
                  // 订阅事件

                  if (btnProps.subEvents) {
                    _forEach(btnProps.subEvents, function (subEvent) {
                      var _split2 = _split(subEvent, subEventSep),
                          _split3 = _slicedToArray(_split2, 2),
                          subEventListenStr = _split3[0],
                          subEventActionStr = _split3[1];

                      var subEventAction = getContextValue(context, subEventActionStr);

                      if (_isFunction(subEventAction)) {
                        modalProps[subEventListenStr] = subEventAction;
                      }
                    });
                  }

                  break;

                case openTypeCode.drawer:
                  // 打开 侧滑Modal
                  modalProps.type = openTypeCode.drawer;
                  modalProps.width = btnProps.openPageTypeDrawer;
                  modalProps.modalBtns = btnProps.modalBtns; // modal 按钮
                  // 订阅事件

                  if (btnProps.subEvents) {
                    _forEach(btnProps.subEvents, function (subEvent) {
                      var _split4 = _split(subEvent, subEventSep),
                          _split5 = _slicedToArray(_split4, 2),
                          subEventListenStr = _split5[0],
                          subEventActionStr = _split5[1];

                      var subEventAction = getContextValue(context, subEventActionStr);

                      if (_isFunction(subEventAction)) {
                        modalProps[subEventListenStr] = subEventAction;
                      }
                    });
                  }

                  break;

                default:
                  break;
              }

              _this2.setState({
                pageModalProps: _objectSpread({
                  pageCode: btnProps.pageCode
                }, modalProps)
              });

              if (_this2.pageModalRef) {
                _this2.pageModalRef.init(params);

                _this2.pageModalRef.show();
              }
            };

            break;

          default:
            break;
        } // TODO Button 的显示属性 最后看在哪里处理


        if (!componentProps.children && componentProps.text) {
          componentProps.children = componentProps.text;
        } else {
          // use [fieldLabelProp] as Button's text
          componentProps.children = field[fieldLabelProp];
        }

        return /*#__PURE__*/React.createElement(React.Fragment, {
          key: field[fieldNameProp]
        }, React.createElement(ComponentType, _objectSpread(_objectSpread({}, componentProps), buttonProps)));
      });
    }
  }]);

  return DynamicToolbar;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handlePageModalRef", [_dec], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageModalRef"), _class2.prototype)), _class2)) || _class);
export { DynamicToolbar as default }; // 暴露出去的方法

DynamicToolbar.exportFuncs = [];
DynamicToolbar.exportFuncsInfo = {};